package com.lanou.tipsy.tools.download.bean;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.widget.RemoteViews;
import com.lanou.tipsy.R;
import com.lanou.tipsy.fragment.rank.RankInfoFragment;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/***
 * 处理通知栏的布局类
 */

public class NotificationBean extends Notification {
    private Context mContext;
    private  Bitmap bitmap;
    private Handler mHandler;

    public NotificationBean(Context context, int icon, CharSequence tickerText, long when,String title , final String pic_url) {
        super(icon, tickerText, when);
        this.mContext = context;
        this.flags = Notification.FLAG_AUTO_CANCEL;
        RemoteViews mRemoteView = new RemoteViews(mContext.getPackageName(), R.layout.remote_view);
        this.contentView = mRemoteView;
        contentView.setTextViewText(R.id.notiNameTv,title);

        //开启一个新线程,用来加载网络图片
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(pic_url);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    if (connection.getResponseCode() == 200) {
                        InputStream is = connection.getInputStream();
                        bitmap = BitmapFactory.decodeStream(is);
                        contentView.setImageViewBitmap(R.id.notiImageIv, bitmap);
                        is.close();
                        connection.disconnect();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();



        Intent intent = new Intent(mContext, RankInfoFragment.class); // 点击
        //intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        this.contentIntent = pIntent;

    }

}
